var webpack = require('webpack');
var htmlWebpackPlugin = require('html-webpack-plugin');
var path = require('path');
var openBrowserPlugin = require('open-browser-webpack-plugin');
var hotMiddlewareScript = 'webpack-hot-middleware/client?';
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var CleanWebpackPlugin = require('clean-webpack-plugin');

var config = {
  entry:{
    "dk":[path.resolve(__dirname,"./src/main.js"),hotMiddlewareScript]
  },
  output:{
    path:path.resolve(__dirname,'./dist'),
    filename:"js/[name]-[hash].js",
    chunkFilename:'js/[name]-[hash].js',
    // publicPath:"../"
  },
  module:{
    rules:[
      {
        test:/\.vue$/,
        use:[
          {
            loader:"vue-loader",
            options:{
              loaders:{
                css:ExtractTextPlugin.extract({
                  use:["css-loader",'postcss-loader'],
									fallback:'vue-style-loader'
                })
              }
            }
          }
        ]
        // use:"vue-loader",
      },
      {
        test:/\.css$/,
        use:ExtractTextPlugin.extract({
          fallback:"style-loader",
          use:["css-loader","postcss-loader"]
        })
      },
      {
        test:/\.js$/,
        use:["babel-loader"],
        exclude:path.resolve(__dirname,'./node_modules')
      },
      {
        test: /\.(svg|woff2?|eot|ttf|otf)(\?.*)?$/,
        // use:['file-loader']
        use: [
          {
            loader: 'url-loader',
            options: {
              limit:10000,
              name: 'fonts/[name].[hash].[ext]'
            }
          }
        ]
      },
      {
        test:/\.(png|jpe?g|gif)(\?.*)?$/,
        use:[{
          loader:"url-loader",
          options:{
            limit:10000,
            name:"images/[name].[hash:7].[ext]"
          }
        }]
      }
    ]
  },
  resolve: {
    alias: {
      'vue$': 'vue/dist/vue.common.js'
    },
    extensions:['.js','.vue','.json','.css']
  },
  plugins:[
    // 使用热更新必要的插件
    new webpack.HotModuleReplacementPlugin(),
    new webpack.NoEmitOnErrorsPlugin(),
    new openBrowserPlugin("http://localhost:8080"),
    new htmlWebpackPlugin({
      template:path.resolve(__dirname,"./index.html"),
      filename:"index.html",
      title:"迪康金诺组件库",
      inject:"body",
      minify:{
        removeComments:true,  // 去掉注释
        collapseWhitespace:true, // 压缩空格
        removeAttributeQuotes:true // 去除属性引用
      }
    }),
    new ExtractTextPlugin("[name]-[hash].css"),
    new CleanWebpackPlugin(["dist"],{
            root: '',  // An absolute path for the root  of webpack.config.js
            verbose: true, // Write logs to console.
            dry: false  // Do not delete anything, good for testing.
    })
  ]
}
module.exports = config;
